package com.wg.core.time;

import cn.hutool.cron.task.Task;

/**
 * 定时任务项：封装单个定时任务的配置和执行逻辑
 *
 * @author 少爷123
 */
public class TimedTask {
    private final String taskId;
    private long nextExecutionTime;
    private final int intervalMs;
    private final Task taskHandler;

    public TimedTask(String taskId, int intervalMs, Task taskHandler) {
        this.taskId = taskId;
        this.intervalMs = intervalMs;
        this.taskHandler = taskHandler;
        this.nextExecutionTime = System.currentTimeMillis() + intervalMs;
    }

    public boolean executeIfDue(long currentTime) {
        if (currentTime >= nextExecutionTime) {
            taskHandler.execute();
            nextExecutionTime = currentTime + intervalMs;
            return true;
        }
        return false;
    }

    public String getTaskId() {
        return taskId;
    }

    public int getIntervalMs() {
        return intervalMs;
    }

    public Task getTaskHandler() {
        return taskHandler;
    }
}